/*----------------------------------------------------------------------
  - File name      : SCCB.h
  - Author         : Liu Chao
  - Update date    : 2020-07-14                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: 
-----------------------------------------------------------------------*/
#ifndef __OV7725_H_
#define __OV7725_H_
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/

#include "main.h"
#include "gpio.h"

/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/

//--OV7725 GPIO PIN define----------------------------------------------

//FIFO Output enable --- OE
#define  OV7725_OE_GPIO_PORT             GPIOB
#define  OV7725_OE_GPIO_PIN              GPIO_PIN_9

//FIFO Write reset --- WRST
#define  OV7725_WRST_GPIO_PORT           GPIOA
#define  OV7725_WRST_GPIO_PIN            GPIO_PIN_15

// FIFO Read reset --- RRST
#define  OV7725_RRST_GPIO_PORT            GPIOB
#define  OV7725_RRST_GPIO_PIN             GPIO_PIN_5

// FIFO Read rclk --- RCLK
#define  OV7725_RCLK_GPIO_PORT            GPIOB
#define  OV7725_RCLK_GPIO_PIN             GPIO_PIN_8

// FIFO Write enable --- WEN
#define  OV7725_WEN_GPIO_PORT              GPIOB
#define  OV7725_WEN_GPIO_PIN               GPIO_PIN_10

// FIFO Write enable --- VSYNC
#define  OV7725_VSYNC_GPIO_PORT            GPIOB
#define  OV7725_VSYNC_GPIO_PIN             GPIO_PIN_0
#define  OV7725_VSYNC_EXTI_IRQ             EXTI0_IRQn
#define  OV7725_VSYNC_IRQHandler           EXTI0_IRQHandler

// 8 bit data port

#define   OV7725_DATA_GPIO_PORT             GPIOA
#define   OV7725_DATA_0_GPIO_PIN            GPIO_PIN_0
#define   OV7725_DATA_1_GPIO_PIN            GPIO_PIN_1
#define   OV7725_DATA_2_GPIO_PIN            GPIO_PIN_2
#define   OV7725_DATA_3_GPIO_PIN            GPIO_PIN_3
#define   OV7725_DATA_4_GPIO_PIN            GPIO_PIN_4
#define   OV7725_DATA_5_GPIO_PIN            GPIO_PIN_5
#define   OV7725_DATA_6_GPIO_PIN            GPIO_PIN_6
#define   OV7725_DATA_7_GPIO_PIN            GPIO_PIN_7


#define   FIFO_OE_H     HAL_GPIO_WritePin(OV7725_OE_GPIO_PORT,OV7725_OE_GPIO_PIN,  GPIO_PIN_SET)
#define   FIFO_OE_L     HAL_GPIO_WritePin(OV7725_OE_GPIO_PORT,OV7725_OE_GPIO_PIN,GPIO_PIN_RESET)

#define   FIFO_WRST_H   HAL_GPIO_WritePin(OV7725_WRST_GPIO_PORT,OV7725_WRST_GPIO_PIN,  GPIO_PIN_SET)
#define   FIFO_WRST_L   HAL_GPIO_WritePin(OV7725_WRST_GPIO_PORT,OV7725_WRST_GPIO_PIN,GPIO_PIN_RESET)

#define   FIFO_RRST_H   HAL_GPIO_WritePin(OV7725_RRST_GPIO_PORT,OV7725_RRST_GPIO_PIN,  GPIO_PIN_SET)
#define   FIFO_RRST_L   HAL_GPIO_WritePin(OV7725_RRST_GPIO_PORT,OV7725_RRST_GPIO_PIN,GPIO_PIN_RESET)

#define   FIFO_RCLK_H   HAL_GPIO_WritePin(OV7725_RCLK_GPIO_PORT,OV7725_RCLK_GPIO_PIN,  GPIO_PIN_SET)
#define   FIFO_RCLK_L   HAL_GPIO_WritePin(OV7725_RCLK_GPIO_PORT,OV7725_RCLK_GPIO_PIN,GPIO_PIN_RESET)

#define   FIFO_WEN_H   HAL_GPIO_WritePin(OV7725_WEN_GPIO_PORT,OV7725_WEN_GPIO_PIN,  GPIO_PIN_SET)
#define   FIFO_WEN_L   HAL_GPIO_WritePin(OV7725_WEN_GPIO_PORT,OV7725_WEN_GPIO_PIN,GPIO_PIN_RESET)

#define READ_FIFO_PIXEL(RGB565)   	do{\
	                                  RGB565 = 0;\
	                                  FIFO_RCLK_L;\
	                                  RGB565 = (OV7725_DATA_GPIO_PORT-> IDR << 8) & 0xff00;\
	                                  FIFO_RCLK_H;\
                                    FIFO_RCLK_L;\
	                                  RGB565 |= (OV7725_DATA_GPIO_PORT->IDR) & 0x00ff;\
	                                  FIFO_RCLK_H;\
                                    }while(0)

#define FIFO_PREPARE                do{\
	                                  FIFO_RRST_L;\
	                                  FIFO_RCLK_L;\
	                                  FIFO_RCLK_H;\
	                                  FIFO_RRST_H;\
	                                  FIFO_RCLK_L;\
	                                  FIFO_RCLK_H;\
                                    }while(0)

//--OV7725 Resolution enum----------------------------------------------

typedef enum
{
    RES_QVGA = 0,  // 240 * 320 px
    RES_VGA     ,  // 480 * 640 px
}   RES_TypeDef;

//--OV7725 Mode Struct init --------------------------------------------

typedef struct
{
    RES_TypeDef IMAGE_RES; //Image resolution

    uint16_t CAMER_RES_Width;//Image resolution width
    uint16_t CAMER_RES_Heigh;//Image resolution heigh

    uint16_t CAMER_SX; //Camera window x start position
    uint16_t CAMER_SY; //Camera window y start position

    uint8_t Light_Mode; //light mode , Parameter range:[ 0 ~  5]
    int8_t Saturation;  //saturation , Parameter range:[-4 ~ +4]
    int8_t Brightness;  //Brightness , Parameter range:[-4 ~ +4]
    int8_t Contrast;    //Contrast   , Parameter range:[-4 ~ +4]
    uint8_t Effect;     //Effect     , Parameter range:[ 0 ~  6]
}   OV7725_ModeTypeDef;

/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/

uint8_t OV7725_Init(void);
void OV7725_Light_Mode(uint8_t mode);
void OV7725_Brightness(int8_t bri);
void OV7725_Contrast(int8_t cnst);
void OV7725_Color_Saturation(int8_t sat);
void OV7725_Brightness(int8_t bri);
void OV7725_Contrast(int8_t cnst);
void OV7725_Special_Effect(uint8_t eff);

void OV7725_Window_Set(uint16_t sx      ,
                       uint16_t sy      ,
                       uint16_t width   ,
                       uint16_t height  ,
                       uint8_t QVGA_VGA);

#endif

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/
